<?php

	require_once(dirname(__FILE__)."/app.render.php");
	
	$function = $_GET['function'];
	
	switch ($function) {   	
		case "calculate-item":
			$base_price = explode('|', $_POST['price']);
    
    		if (count($_POST['options']) > 0) {
    			$options = $_POST['options'];
      		
      		for ($i = 0; $i < count($options); $i++) {
        			$current_option = explode(',', $options[$i]);
        			$prices_array[$i]      = $current_option[1];
        			$prices_array_name[$i] = $current_option[0];
       		} 
      		
      		$return_price_options = implode(',', $prices_array_name);
      		$price = array_sum($prices_array) + $base_price[1] + $_POST['base'];
     			$price2 = price_format($price);
     			
     			echo $price . '|' . $return_price_options . '|' . $base_price[0] . '|' . $price2 . '';
     			
   		} else {
      		$price  = $base_price[1] + $_POST['base'];
      		$price2 = price_format($price);
      		echo $price . '|None|' . $base_price[0] . '|' . $price2 . '';
    		}
    		
    		break;
 			
 		case "add-post":
    		if (isset($_FILES['file']) && $_FILES['file']['name'] != "") {
      		$target_path = "UserFiles/File/customers/";
      		$filename    = '' . rand(10000000, 100000000000000) . '.' . $_FILES['file']['name'] . '';
      		$target_path = $target_path . $filename;
      	
      		if (move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
        			$file = $filename;
      		} else {
        			$file = '';
      		}
      		
    		} else {
      		$file = NULL;
    		}

    		if (isset($_REQUEST['price'])){
      		$price = $_REQUEST['price'];
    		} else {
      		$price = 0;
    		}

    		$price     = explode("|", $price);
    		$mainprice = $price[1] + $options_price;
			
    		if (isset($_REQUEST['options'])){
      		$options = $_REQUEST['options'];
    		} else {
      		$options = Array();
    		}

    		$i = 0;
    		if (count($options) > 0) {
      		foreach ($options as $key => $value) {
        			$i++;
        			$pieces = explode(",", $value);
        			$alloptions[$i] = $pieces[0];
        		} 
    		} 

    		if (isset($_REQUEST['final_price'])){
      		$final_price = $_REQUEST['final_price'];
    		} else {
      		$final_price = 0;
    		}

    		if (!isset($opts_price)){
      		$opts_price = 0;
    		}
    
    		$total = $opts_price + $final_price;
    		
    		$cart_object->addItem(new faucartelement(treatGet($_REQUEST['name']), treatGet('<b>' . $lang['size'] . ':</b> ' . $_REQUEST['final_price_type'] . ' <br>
                      <b>' . $lang['instructions'] . '</b>: ' . $_REQUEST['instructions'] . '<br>
                      <b>' . $lang['options'] . ':</b> ' . @implode(" ", $alloptions) . '' . $toppings_multi . '' . $base_toppings . ''), treatGet($total), treatGet($_REQUEST['quantity']), NULL, NULL, $discount, $file));
    		
    		echo '<p>' . treatGet($_REQUEST['name']) . ' ' . $lang['cart_added'] . '</p>';
    		
    		toSession();
    		
    		break;
    		
		case "add-carrito":			
			$itemId = $_POST['id'];
			$precio = $_POST['precio'];
			$nombre = $_POST['nombre'];
			$cantidad = $_POST['cantidad'];
						
			$cart_object->addElement( new itemTemporal($itemId, $nombre, $precio, $cantidad) );
												
			toSession();
			
			header("Location: " . $_SERVER['HTTP_REFERER']);
			
    		break;    	

		//FUNCION AGREGADA  		
		case "delTemp":
			$cart_object->delElement($_GET['idB']);
			toSession();
			header("Location: " . $_SERVER['HTTP_REFERER']);
			break;  		
  		
  		case "del":
    		$cart_object->delItemByObjectID($_GET["id"]);
    		toSession();
    		break;
  		
		case "empty":
    		$cart_object->emptyCart();
   		toSession();
    		break;
    		  		
  		case "cart":
    		$cart_object->total();
    		$total      = $cart_object->_total;
    		$cart_items = $cart_object->getItems();
    		
    		function isEmpty($array = array()){
      		foreach ($array as $element) {
        			if (!empty($element)) {
          			return false;
        			}
      		}
      		return true;
    		}
    
    		if (isEmpty($cart_items) == true) {
    			echo 'hola';
      		$minicart = $lang['cart_empty'];
    		} else {
    			$minicart = '
    			<table width="95%"  border="0" align="center" cellpadding="2" cellspacing="2" id="minicart">
    				<tr>
    					<td>
    					</td>
    					
    					<td id="cartop" align="center">
    						<span style="font-size:0.8em; font-weight:bold;">
    							Cant.
    						</span>
    					</td>
    					    					
    					<td id="cartop" align="center">
    						<span style="font-size:0.8em; font-weight:bold;">
    							'. $lang['cart_name'] .'
    						</span>
    					</td>
    					
    					<td id="cartop" align="center">
    						<span style="font-size:0.8em; font-weight:bold;">
    							'. $lang['cart_price'] .'
    						</span>
    					</td>
    				</tr>';
    				
    				foreach ($cart_items as $cart_item) {
        				$minicart .= "<tr>";
        				
        				if (isset($_GET['pid'])) {
          				$pidParam = ", " . $_GET['pid'];
        				} else {
          				$pidParam = "";
        				}
        				
        				$minicart .= "
        				<td>
        					<a href='ajax.php?function=delTemp&idB=". $cart_item->item_id ."' > 
      							<img border='0' src='images/basket_delete.png' />
    							</a>
        				</td>
        				
        				<td align='center' >    						
    						<span style='font-size:0.8em;'>
    							<i>". $cart_item->quantity ."</i>
    						</span>
    					</td>
    					
    					<td align='center'>
    						<span style='font-size:0.8em;'>
    							<i>". $cart_item->name ."</i>
    						</span>
    					</td>
    					
    					<td align='center' id=\"minicartitems\">
       					<span style='font-size:0.8em;'>
       						<i>" . price_format($cart_item->price) . "</i>
       					</span>
       				</td>";
       				
        				$minicart .= "</tr>";
      			} 
      			
      			$minicart .= '
      			<tr>
      				<td colspan="4">							    					
      					<hr>
      				</td>
      			</tr>
      			
      			<tr>
      				<td align="right" colspan="3" id="cartop">
      					<span style="font-weight:bold;">
      						'. $lang['cart_total'] .'
      					</span>
      				</td>
    					
    					<td align="center" id="cartop">
    						<span style="font-weight:bold;">
    							'. price_format($total) .'
    						</span>
    					</td>
    				</tr>
    				</table>';
    				
    				$minicart .='
    				<table width="95%"  border="0" align="center" cellpadding="2" cellspacing="2">
    					<tr>
    						<td>
    							<div align="center">
	 								<span>
	 								
	 								</span>
	 							</div>
	 						</td>
	 					
	 						<td>
	 							<div align="center">
		 							<span style="margin-left:10px;float: right;
padding-top: 1px;
background: url(http://www.comprita.com/images/btn_order_lg.jpg) repeat-x 0 0;
width: 85px;
height: 26px;
text-align: center;
font-size: 17px;
color: #CC7900;
border: 3px solid #E1AC51;
font-weight: bold;
text-shadow: white 1px 1px 0px;
cursor: pointer;
padding-top: 6PX;
margin-top: 10px;">
    									<a href="sites.php?act=checkout">
    										<font color="#cc7900">'. $lang['checkout_button'] .' </font>
    									</a>
    								</span>
    							</div>
    						</td>
  						</tr>
  					</table>';
    		}
    		
    		echo $minicart;
   		break;
		
		case "add-post-clone":
			$results_inv_clone = $database->select("SELECT * FROM order_inv where oid = " . $_GET['clone_id'] . "");
			
			for ($n = 0; $n < count($results_inv_clone); $n++) {
      		$cart_object->addItem(new faucartelement(treatGet($results_inv_clone[$n]['item_name']), treatGet(html_entity_decode($results_inv_clone[$n]['item_description'])), treatGet($results_inv_clone[$n]['item_price']), treatGet($results_inv_clone[$n]['item_quantity']), treatGet($results_inv_clone[$n]['iid']), treatGet($results_inv_clone[$n]['item_options'])));
      	} 
    		
    		toSession();
    		header("Location:sites.php?act=checkout");
    		break;
	} //$function
?>
